package com.bdmc.smokesensor.services;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import com.bdmc.smokesensor.dao.Sys_DepartmentJPA;
import com.bdmc.smokesensor.entity.Sys_Department;
import com.bdmc.smokesensor.iservices.ISys_DepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

@Service
public class Sys_DepService extends BaseService<Sys_DepService> implements ISys_DepService {

@Autowired
public Sys_DepartmentJPA _depJpa;
    @Override
    public List<Sys_Department> findAll() {
        return _depJpa.findAll();
    }

    @Override
    public Map<String, Object> findbyPageable(int depType,int id,int page, int rows) {
		Page<Sys_Department> p = _depJpa.findByDepTypeAndTreeRootContainingOrId(depType,","+id+",",id ,PageRequest.of(page,rows));
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("total", p.getTotalElements());
        map.put("data", p.getContent());
       return map; 
	}

    @Override
    public Sys_Department findOne(Example<Sys_Department> example) {
        return _depJpa.findOne(example).orElse(null);    
    }

    @Override
    public Sys_Department findOneById(int id) {
        return _depJpa.findById(id).orElse(null);
    }

    @Override
    public Sys_Department save(Sys_Department d) {
        if (d.getId() < 1)
            d.setJoinDate(new Date());
        else
            d.setUpdateDate(new Date());
        return _depJpa.save(d);
    }

	@Override
	public void deleteById(int id) {
        _depJpa.deleteById(id);
    }

    @Override
    public List<Sys_Department> findByDepTypeAndPidNot(int depType, int pid) {
        return _depJpa.findByDepTypeAndPidNot(depType, pid);
    }

    @Override
    public List<Map<String,Object>> findByDepTypeAndTreeRootLike(int depType, String treeRoot) {
        return _depJpa.findByDepTypeAndTreeRootLike(depType, treeRoot);
    }

    @Override
    public List<Sys_Department> findByDepTypeAndPid(int depType, int pid) {
        return _depJpa.findByDepTypeAndPid( depType , pid);
    }

    @Transactional
    @Override
    public List<Sys_Department> batchSave(List<Sys_Department> entities) {
        return _depJpa.saveAll(entities);
    }

}